package test;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import aorey.database.DataBaseFactory;
import aorey.nosql.EhcacheUtil;

/**
 * Servlet implementation class db
 */
@WebServlet("/db")
public class db extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public db() {
        super();
        
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.getWriter().append("Served at: ").append(request.getContextPath());
		// Aorey Toy mini 连接池测试
		// 数据库操作对象
		DataBaseFactory db = new DataBaseFactory();
		// 提取资源,所有的Aorey Toy框架资源都会存在这里(连接池,SQL配置缓存,模板对象......)
		EhcacheUtil framework = (EhcacheUtil) request.getServletContext().getAttribute("aorey");
		// 获取数据连接池资源
		HashMap<String, DataSource> dataBaseDrivers = (HashMap<String, DataSource>) framework.get("database", "drivers");
		try {
			// 从连接池管理器获取一个pgsql连接
			db.getConnection(dataBaseDrivers.get("pgsql").getConnection());
			// 执行SQL语句,结果为JSON数组,后期会从配置读取
			String rs = db.getResult("SELECT id, f1, f2 FROM public.test");
    		System.out.println(rs);
			
		} catch (Exception e) {		
		}
		finally {
			// 关闭连接池,不关闭将会导致连接数上限
			db.close();
		}
		
		
		
		
		
	}

}
